import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingWorker;

public class View extends WindowAdapter implements Observer
{
	private Model model;
	public MainFrame mf;
	ConsolePanel console;
	CreateFaculty cf;
	int totalQueries;
	int currentQuery;
	ProgressFrame progframe;
	/*
	 * Constructor
	 */
	public View(Model m)
	{
		this.setModel(m);
	}
	
	public void displayProgress()
	{
		progframe = new ProgressFrame();
	}
	
	public void init()
	{
		setMainFrame(new MainFrame(this));
		//update(getModel());
		getMainFrame().setVisible(true);
		cf = new CreateFaculty();
		cf.init();
		cf.setVisible(false);
		
		setConsole(new ConsolePanel());
		getConsole().setVisible(false);
	}
	
	public String getCollege()
	{
		return cf.getCNameText().getText();
	}
	
	public String getFaculty()
	{
		return cf.getNameText().getText();
	}
	
	public String getField()
	{
		return cf.getAreas().getSelection().getActionCommand();
	}
	
	public String getAddress()
	{
		return cf.getAddText().getText();
	}
	
	public JFrame getConsole()
	{
		return console;
	}
	
	public void addConsoleListener(ActionListener al)
	{
		this.getMainFrame().console.addActionListener(al);
	}
	public void addFacultyListener(ActionListener al)
	{
		this.cf.getCreate().addActionListener(al);
	}
	
	public void addQueryListener(ActionListener al)
	{
		this.getMainFrame().getGo().addActionListener(al);
	}
	
	public void addSelectionListeners(ActionListener al)
	{
		this.getMainFrame().getFirst().collegeBox.addActionListener(al);
		this.getMainFrame().getSecond().collegeBox.addActionListener(al);
	}
	
	public void addQuitListener(ActionListener al)
	{
		this.getMainFrame().quit.addActionListener(al);
	}

	public void addFaculty()
	{
		JFrame faculty = new CreateFaculty();
		faculty.setAlwaysOnTop(true);
		faculty.setVisible(true);
	}
	public void quit(MainFrame mainFrame) 
	{
		
	}
	
	/* (non-Javadoc)
	 * @see Observer#update(Subject)
	 */
	@Override
	public void update(Subject s) 
	{	
		String [] collegeList;
		ArrayList<College> updated;
		updated = s.getColleges();
		collegeList = new String[updated.size()+1];
		collegeList[0] = "Select College";
		Iterator<College> iter = updated.iterator();
		int count = 1;
		while(iter.hasNext())
		{
			collegeList[count] = iter.next().getCollegeName();
			System.out.println(collegeList[count]);
			count++;
		}
		this.getMainFrame().setColleges(collegeList);
		
		System.out.println("updating");
		this.getMainFrame().updateDropdowns();
	}

	public void setMainFrame(MainFrame mf) 
	{
		this.mf = mf;
	}

	public MainFrame getMainFrame()
	{
		return mf;
	}
	
	public void drawChart(Faculty selected1, Faculty selected2) 
	{	
		mf.getChart().setFirstInitials(getInitials(mf.getFirst().getSelectedCollege()));
		mf.getChart().setSecondInitials(getInitials(mf.getSecond().getSelectedCollege()));
		
		mf.getChart().setFirstName(selected1.getName());
		mf.getChart().setSecondName(selected2.getName());
		
		mf.getChart().setFirstPapers(selected1.getTotalFacultyPapers());
		mf.getChart().setSecondPapers(selected2.getTotalFacultyPapers());
		
		mf.getChart().setFirstCitations(selected1.getTotalFacultyCitations());
		mf.getChart().setSecondCitations(selected2.getTotalFacultyCitations());
		
		
		mf.getChart().setSelectionMade(true);
		mf.repaint();
	}

	public void reset()
	{
		
	}
	
	public String getInitials(String str)
	{
		//get initials by taking first char & every one preceded by whitespace
		int of = str.indexOf(" of");
		String temp = "";
		if(of != -1)
		{
			temp += str.substring(0, of);
			temp += str.substring(of+3);
			
			str = temp;
		}
		String newStr = "";
		newStr += str.charAt(0);
		for(int i = 1; i<str.length(); i++)
		{
			if(str.length() >= (i+1) && Character.isWhitespace(str.charAt(i)))
			{
				newStr+= str.charAt(i+1);
			}
		}
		
		newStr.toUpperCase();
		return newStr;
	}

	public void setModel(Model model)
	{
		this.model = model;
	}

	public Model getModel()
	{
		return model;
	}
	
	class ProgressFrame extends JFrame
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = -6262842038653038998L;
		JFrame progframe;
		public ProgressFrame()
		{
			progframe = new JFrame("Please Wait...");
	        progframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	        progframe.setLayout(new FlowLayout());
	        progframe.add(new JLabel("Querying Faculties"));
	        ImageIcon icon = new ImageIcon("load.gif");
	        JLabel image = new JLabel(icon);
	        progframe.getContentPane().add(image);
	        
	        //Display the window.
	        progframe.pack();
	        progframe.setVisible(true);
	        this.setAlwaysOnTop(true);
		}
		
		public void dispose() 
		{
			progframe.dispose();
		}
	}

	public void setConsole(ConsolePanel console)
	{
		this.console = console;
	}
}
